package nsga;

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import nsga.Nsga2Algorithm.Nsga2Parameters;

import common.CommonHelper;
import common.GeneticHelper;
import common.GeneticOperations;

public abstract class NsgaRunner<Genotype> {
    
    public abstract GeneticOperations<Genotype> getGeneticOperations(Properties properties, Nsga2Parameters params);
    
    public String getPrefix(String inputFile, String problemName) {
        
        String prefix = CommonHelper.fromInputfilenameToPrefix(inputFile);
        prefix = "output/"+prefix+"_nsga_";
        return prefix;
    }
    
    public void start(String problemName, String[] args) {
        
        CommonHelper.printAssertionState();
        System.out.println();
        System.out.println("NSGA "+problemName+" Witaj");
        
        if (args.length != 1) {
            System.out.println("Parametry: [properites_z_wlasciwosciami]");
            System.exit(1);
        } else {
            System.out.println("Wczytuje properties: "+args[0]);
        }
        CommonHelper.sleep(1500);
               
        Properties properties = CommonHelper.readProperties(args[0]);
        Nsga2Parameters params = NsgaHelpers.getElEmasParameters(properties);
        GeneticOperations<Genotype> go = getGeneticOperations(properties, params);
        String prefix = getPrefix(params.inputFile,problemName.toLowerCase());
        
        System.out.println("prefix dla plików wynikowych: "+prefix);
    
        List<Double> ratio = new LinkedList<Double>();
        int it;
        for (it = 0; it < 1; it++) {
                  
            Nsga2Algorithm<Genotype> pr = new Nsga2Algorithm<Genotype>(go,params);

            
            String randomPopulation = GeneticHelper.populationValuesToString(pr.getPopulation());
            CommonHelper.printToFile(prefix+"random.txt", randomPopulation);
    
            pr.runExperiment();
            
            GeneticHelper.savePopulationToFile(prefix+"all", pr.getPopulation());
            GeneticHelper.savePopulationToFile(prefix+"bests", pr.getAllNonDominated());
            
            CommonHelper.printToFile(prefix+"hv.txt", pr.getHvInTime());
                  
            pr.printExperimentDetails();
        }
        double sum = 0.0;
        for (Double r : ratio) {
            sum += r;
        }
        double result = sum / it;
        System.out.println("result: "+result);
        
        
    }
}